package com.gonsin.gview.example.gview.user;

import com.gonsin.base.GPageRequest;
import com.gonsin.base.GPageResult;
import com.gonsin.gview.annotations.GView;
import com.gonsin.gview.annotations.actions.GAction;
import com.gonsin.gview.annotations.actions.GSession;
import com.gonsin.gview.annotations.board.GBoard;
import com.gonsin.gview.annotations.board.GButton;
import com.gonsin.gview.annotations.board.form.*;
import com.gonsin.gview.annotations.board.table.*;
import com.gonsin.gview.annotations.data.*;
import com.gonsin.gview.annotations.enums.ButtonAction;
import com.gonsin.gview.annotations.enums.ColumnType;
import com.gonsin.gview.annotations.model.DownloadResponse;
import com.gonsin.gview.example.bean.UserBean;
import com.gonsin.gview.example.dao.UserDao;
import com.gonsin.gview.example.utils.PageableUtils;
import com.gonsin.gview.logic.enums.FileUploadType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@GView(
        viewName = "UserView",
        title = "用户列表",
        boards = {
                @GBoard(table = @GTable(
                        tableData = @GTableData(fromDataSource = "searchUser", keyName = "userId"),
                        header = @GHeader(
                                buttons = {
                                        @GButton(name = "添加子账号", action = ButtonAction.VIEW_BY_DIALOG, view = "AddUserView", icon = "el-icon-plus"),
                                        @GButton(name = "导入", action = ButtonAction.UPLOAD_FILE, actionName = "uploadUser"),
                                        @GButton(name = "上传文件夹", action = ButtonAction.UPLOAD_FILE, actionName = "uploadUser", fileUploadType = FileUploadType.PATH),
                                        @GButton(name = "下载图片", action = ButtonAction.DOWNLOAD, actionName = "downloadTest"),
                                        @GButton(name = "下载文本", action = ButtonAction.DOWNLOAD, actionName = "downloadTest2"),
                                        @GButton(name = "导出", action = ButtonAction.DOWNLOAD, downloadUrl = "/restapi/s08/download?url=xx"),
                                        @GButton(name = "批量删除", action = ButtonAction.BATCH, actionName = "batchDeleteUser"),
                                        @GButton(permission = "not_show", name = "这个按钮不显示", action = ButtonAction.BATCH, actionName = "batchDeleteUser")
                                },

                                searchBar = @GSearchBar(
                                        filters = {
                                                @GSearchFilter(
                                                        selector = @GSelector(
                                                                name = "用户组",
                                                                keyName = "groupId",
                                                                listData = @GListData(
                                                                        keyName = "groupId",
                                                                        valueName = "name",
                                                                        fromSql = "select id as groupId, name from gs_user_group"))),
                                                @GSearchFilter(timeSelector = @GTimeSelector(keyName = "startTime", name = "开始时间")),
                                                @GSearchFilter(dateSelector = @GDateSelector(keyName = "startDate", name = "开始日期")),
                                                @GSearchFilter(dateTimeSelector = @GDateTimeSelector(keyName = "startDateTime", name = "开始日期和时间")),
                                                @GSearchFilter(textInput = @GTextInput(name = "用户名", keyName = "realName")),
                                                @GSearchFilter(
                                                        textInput = @GTextInput(name = "用户名（下拉）", keyName = "realName", simpleListData = @GSimpleListData(fromJson = "['李卓轩','邓文定']"))),
                                        }
                                )
                        ),
                        count = 10,
                        columns = {
                                @GColumn(name = "权限组", keyName = "groupName", weight = 1),
                                @GColumn(name = "真实名字", keyName = "realName", weight = 1),
                                @GColumn(name = "用户名", keyName = "username", weight = 1),
                                @GColumn(name = "冷冻", columnType = ColumnType.FORM_ITEM,
                                        formItems = @GFormItem(gswitch = @GSwitch(keyName = "enable", onChangeAction = "updateUser"))),
                                @GColumn(name = "操作", columnType = ColumnType.BUTTONS, weight = 1, buttons = {
                                        @GButton(name = "修改", action = ButtonAction.VIEW_BY_DIALOG, view = "UpdateUser",
                                                props = @GProp(keyName = "userId", valueName = "userId")),
                                        @GButton(name = "修改密码", action = ButtonAction.VIEW_BY_DIALOG, view = "UpdateUserPassword",
                                                props = @GProp(keyName = "userId", valueName = "userId")),
                                        @GButton(name = "删除", action = ButtonAction.CONFIRM, actionName = "deleteUser",
                                                props = @GProp(keyName = "userId", valueName = "userId"),
                                                confirmMessage = "您确定要删除该账号吗？"),
                                }),
                        }
                )),
        }
)
@Component
@Slf4j
public class UserView {

    @Autowired
    private UserDao userDao;

    /**
     * 查询人员
     */
    @GDataSource("searchUser")
    public GPageResult<UserBean> searchUser(@GSession("userId")Integer userId,
                                            GPageRequest pageRequest,
                                            @GParam("startTime") Long startTime,
                                            @GParam("startDate") Long startDate,
                                            @GParam("startDateTime") Long startDateTime){

        // 自行实现查询操作
        Page<UserBean> users = userDao.findAll(PageableUtils.toPageable(pageRequest));
        return PageableUtils.toGPageResult(users);
    }

    /**
     * 删除用户
     */
    @Transactional
    @GAction("deleteUser")
    public void deleteUser(int userId){
        userDao.deleteById(userId);
    }


    /**
     * 批量删除用户组
     */
    @Transactional
    @GAction("batchDeleteUser")
    @GDataChanged({"getUser", "searchUser"})
    public void batchDeleteUser(List<Integer> userIds){
        userDao.deleteAllIn(userIds);
    }

    /**
     * 上传excel文件导入用户
     */
    @Transactional
    @GAction("uploadUser")
    @GDataChanged({"searchUser"})
    public void uploadUser(HttpSession session, File file){
        log.info("uploadUser file " + file.exists());
    }

    /**
     * 导出成excel
     */
    @Transactional
    @GAction("downloadTest")
    public DownloadResponse downloadTest(){
        return DownloadResponse.createByUrl("图片.png", "http://192.168.2.179:9080/@fs/E:/work/git/gview-next/packages/components/GLayout/assets/logo.png");
    }

    /**
     * 导出成excel
     */
    @Transactional
    @GAction("downloadTest2")
    public DownloadResponse downloadTest2(){
        return DownloadResponse.createByText("文本.txt", "这是一段文件内容。");
    }
}
